package com.sceneworker.extensions.pqtorus;

import com.acarter.propertytable.Property;
import com.acarter.propertytable.PropertySection;
import com.acarter.propertytable.propertyobject.FloatPropertyObject;
import com.acarter.propertytable.propertyobject.IntegerPropertyObject;
import com.jme.scene.Spatial;
import com.jme.scene.shape.PQTorus;
import com.jme2utils.staticutils.GenericJMEUtils;
import com.sceneworker.extensions.trimesh.TriMeshPropertyPage;

public class PQTorusPropertyPage extends TriMeshPropertyPage {

	public PQTorusPropertyPage() {
		super();
		
		PropertySection		lcl_section = new PropertySection("PQ Torus");
		lcl_section.addProperty(new Property("p", new FloatPropertyObject()));
		lcl_section.addProperty(new Property("q", new FloatPropertyObject()));
		lcl_section.addProperty(new Property("Radius", new FloatPropertyObject()));
		lcl_section.addProperty(new Property("Width", new FloatPropertyObject()));
		lcl_section.addProperty(new Property("Steps", new IntegerPropertyObject()));
		lcl_section.addProperty(new Property("Radial Samples", new IntegerPropertyObject()));
		
		model.addPropertySection(0, lcl_section);
	}
	
	@Override
	protected void updateListeners(final Spatial spatial) {

		super.updateListeners(spatial);
		
		if(!(spatial instanceof PQTorus)) { return; }
		
		final	PQTorus	lcl_torus = (PQTorus)spatial;

		PropertySection		lcl_section = model.getPropertySection("PQ Torus");
		
		FloatPropertyObject		lcl_p = (FloatPropertyObject)lcl_section.getProperty("p").getPropertyObject();
		lcl_p.SetListener(new FloatPropertyObject.I_FloatPropertyObjectListener() {

			public float readValue() {
				return lcl_torus.getP();
			}

			public void saveValue(float value) {
				GenericJMEUtils.updateGeometry(lcl_torus, value, lcl_torus.getQ(), lcl_torus.getRadius(), lcl_torus.getWidth(), lcl_torus.getSteps(), lcl_torus.getRadialSamples());
			}

		});
		
		FloatPropertyObject		lcl_q = (FloatPropertyObject)lcl_section.getProperty("q").getPropertyObject();
		lcl_q.SetListener(new FloatPropertyObject.I_FloatPropertyObjectListener() {

			public float readValue() {
				return lcl_torus.getQ();
			}

			public void saveValue(float value) {
				GenericJMEUtils.updateGeometry(lcl_torus, lcl_torus.getP(), value, lcl_torus.getRadius(), lcl_torus.getWidth(), lcl_torus.getSteps(), lcl_torus.getRadialSamples());
			}

		});
		
		FloatPropertyObject		lcl_radius = (FloatPropertyObject)lcl_section.getProperty("Radius").getPropertyObject();
		lcl_radius.SetListener(new FloatPropertyObject.I_FloatPropertyObjectListener() {

			public float readValue() {
				return lcl_torus.getRadius();
			}

			public void saveValue(float value) {
				GenericJMEUtils.updateGeometry(lcl_torus, lcl_torus.getP(), lcl_torus.getQ(), value, lcl_torus.getWidth(), lcl_torus.getSteps(), lcl_torus.getRadialSamples());
			}

		});
	
		FloatPropertyObject		lcl_width = (FloatPropertyObject)lcl_section.getProperty("Width").getPropertyObject();
		lcl_width.SetListener(new FloatPropertyObject.I_FloatPropertyObjectListener() {

			public float readValue() {
				return lcl_torus.getWidth();
			}

			public void saveValue(float value) {
				GenericJMEUtils.updateGeometry(lcl_torus, lcl_torus.getP(), lcl_torus.getQ(), lcl_torus.getRadius(), value, lcl_torus.getSteps(), lcl_torus.getRadialSamples());
			}

		});
		
		IntegerPropertyObject		lcl_steps = (IntegerPropertyObject)lcl_section.getProperty("Steps").getPropertyObject();
		lcl_steps.SetListener(new IntegerPropertyObject.I_IntegerPropertyObjectListener() {

			public int readValue() {
				return lcl_torus.getSteps();
			}

			public void saveValue(int value) {
				GenericJMEUtils.updateGeometry(lcl_torus, lcl_torus.getP(), lcl_torus.getQ(), lcl_torus.getRadius(), lcl_torus.getWidth(), value, lcl_torus.getRadialSamples());
			}

		});
		
		IntegerPropertyObject		lcl_radialSamples = (IntegerPropertyObject)lcl_section.getProperty("Radial Samples").getPropertyObject();
		lcl_radialSamples.SetListener(new IntegerPropertyObject.I_IntegerPropertyObjectListener() {

			public int readValue() {
				return lcl_torus.getRadialSamples();
			}

			public void saveValue(int value) {
				GenericJMEUtils.updateGeometry(lcl_torus, lcl_torus.getP(), lcl_torus.getQ(), lcl_torus.getRadius(), lcl_torus.getWidth(), lcl_torus.getSteps(), value);
			}

		});
	}
}
